package com.dragon.www.smarthome.popupwindow;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.activity.MainActivity;
import com.dragon.www.smarthome.adapter.AirconditionAdapter;

/**
 * Created by 51268 on 2016/3/14.
 */
public class AirconditionPopWindow extends BasePopupWindow {
    private ImageView imageBackAircondition;

    private boolean isContinue = true;
    public AirconditionPopWindow(Context context) {
        super(context);
        backgroundColor = 0xD9289755;
    }
    private final Handler viewHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    refreshUI();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void showPopWindow(View view ,int xOffsetToView, int yOffsetToView) {
        contentView = LayoutInflater.from(context).inflate(R.layout.aircondition_view, null);
        //显示
        super.showPopWindow(view, xOffsetToView, yOffsetToView);

        imageBackAircondition = (ImageView) contentView.findViewById(R.id.image_back_aircondition);

        bindAdapter(contentView);

        imageBackAircondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                isContinue = false;
            }
        });
        isContinue = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isContinue) {
                    viewHandler.sendEmptyMessage(0);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }).start();
    }

    /**
     * 每隔500毫秒刷新UI
     * 2016/06/01
     */
    private void refreshUI() {
        getValue("aircondition");
    }

    public void bindAdapter(View view) {
        gview = (GridView) view.findViewById(R.id.gview);
        mAdapter = new AirconditionAdapter(context);
        mAdapter.setData(getData(MainActivity.TYPE_AIR));
        gview.setAdapter(mAdapter);
//        getValue("aircondition");
    }
}
